package com.lei.chinese_herbal_medicine.Service.Impl;

import com.lei.chinese_herbal_medicine.Entity.Herb;
import com.lei.chinese_herbal_medicine.Entity.HerbType;
import com.lei.chinese_herbal_medicine.Entity.ModelHerbLink;
import com.lei.chinese_herbal_medicine.Mapper.HerbMapper;
import com.lei.chinese_herbal_medicine.Mapper.HerbTypeMapper;
import com.lei.chinese_herbal_medicine.Mapper.ModelHerbLinkMapper;
import com.lei.chinese_herbal_medicine.Service.HerbTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (HerbType)表服务实现类
 *
 * @author makejava
 * @since 2024-01-18 10:06:42
 */
@Service("herbTypeService")
public class HerbTypeServiceImpl implements HerbTypeService {
    @Resource
    private HerbTypeMapper herbTypeMapper;
    @Resource
    private ModelHerbLinkMapper modelHerbLinkMapper;
    @Resource
    private HerbMapper herbMapper;
    public List<HerbType> selectByHerbType(HerbType record){
        return herbTypeMapper.selectByHerbType(record);
    }

    @Override
    public boolean deleteByPrimaryKey(String id) {
        return herbTypeMapper.deleteByPrimaryKey(id)==1;
    }

    @Override
    public Map selectHerbTypeByModelId(String modelId) {
        List<HerbType> herbTypes=herbTypeMapper.selectByHerbType(new HerbType());
        List<ModelHerbLink> modelHerbLinks=modelHerbLinkMapper.selectByModelId(modelId);
        Herb herb=new Herb();
        Map<String,Integer> count=new HashMap<>();
        for (ModelHerbLink modelHerbLink : modelHerbLinks) {
            herb.setId(modelHerbLink.getHerbId());
            List<Herb> herbs=herbMapper.selectHerbsByHerbAndPageData(herb,null);
            if(herbs.size()==1){
                String herbTypeId=herbs.get(0).getHerbTypeId();
                if(count.get(herbTypeId)==null){
                    count.put(herbTypeId,1);
                }else{
                    count.put(herbTypeId,count.get(herbTypeId)+1);
                }

            }
        }
        Map<String,Integer> herbTypeData=new HashMap<>();
        for (HerbType herbType : herbTypes) {
            Integer num=count.get(herbType.getId());
            if(num==null){
                num=0;
            }
            herbTypeData.put(herbType.getTypeName(),num);
        }
        return herbTypeData;
    }

    /**
     * 新增中草药功效类别
     * @param list
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public boolean insertHerbType(List<HerbType> list){
        boolean flag=false;
        int num=0;
        HerbType herbTypeObj=new HerbType();
        for (HerbType herbType : list) {
            herbTypeObj.setTypeName(herbType.getTypeName());
            if(herbTypeMapper.selectByHerbType(herbTypeObj).size()==0){
                herbTypeMapper.insert(herbType);
                num++;
            }
        }
        flag=(num==list.size());
        if (flag){
            return flag;
        }else{
            throw new RuntimeException("新增中草药种类已存在或其他错误");
        }
    }
}


